# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 13:55:28 2018

@author: mojm
"""

import numpy as np
import sklearn.datasets
import matplotlib.pyplot as plt
from sklearn import neighbors  

def plot_decision_boundary(pred_func):  
    # 设定最大最小值，附加一点点边缘填充  
    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5  
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5  
    h = 0.01  
  
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))  
  
    # 用预测函数预测一下  
    Z = pred_func(np.c_[xx.ravel(), yy.ravel()])  
    Z = Z.reshape(xx.shape)  
  
    # 然后画出图  
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)  
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)

n_samples=1000

X, y = sklearn.datasets.make_moons(n_samples, noise=0.20)

sampleRatio = 0.9
targetRatio = round(1-sampleRatio, 2)
sampleBoundary = int(n_samples*sampleRatio)

training_features = X[:sampleBoundary]
training_targets = y[:sampleBoundary]

test_features = X[sampleBoundary:]
test_targets = y[sampleBoundary:]

clf = neighbors.KNeighborsClassifier()
clf = clf.fit(training_features, training_targets)

plot_decision_boundary(lambda x: clf.predict(x))
plt.scatter(X[:,0], X[:,1], s=40, c=y, cmap=plt.cm.Spectral)

predict_targets = clf.predict(test_features)

accuracy = sklearn.metrics.accuracy_score(test_targets, predict_targets)
print ('accuracy is ...' + str(accuracy))


distances, indices = clf.kneighbors(X)  
print ('indices is ...' + str(indices)) # Indices of the nearest points in the population matrix.
print ('distances is ...' + str(distances)) # Array representing the lengths to points, only present if return_distance=True